package com.system.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.dao.SysPageDao;
import com.system.dao.SysRoleDao;
import com.system.dao.SysUserDao;
import com.system.druid.DataSource;
import com.system.druid.DataSourceType;
import com.system.entity.manage.SysPage;
import com.system.entity.manage.SysUser;
import com.system.entity.manage.UserDataVO;
import com.system.entity.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 系统用户业务层
 *
 * @author 佛冷
 * @date 2019/12/19
 */
@Service
public class UserService {

    @Autowired
    private SysUserDao userDao;
    @Autowired
    private SysRoleDao mgrRoleDao;
    @Autowired
    private SysPageDao sysPageDao;

    /**
     * 验证用户信息
     *
     * @author 佛冷
     * @date 2020/7/3 16:29
     */
    @DataSource(DataSourceType.MASTER)
    public int verifyUserInfo(String username, String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser sysUser = userDao.selectOne(queryWrapper);
        if (sysUser == null) {
            return 1;
        }
        if(sysUser.getStatus() != 0){
            return 2;
        }
        if (!password.equalsIgnoreCase(sysUser.getPassword())) {
            return 3;
        }

        return 0;
    }

    /**
     * 获取用户登录信息
     *
     * @author 佛冷
     * @date 2019/12/19
     */
    @DataSource(DataSourceType.MASTER)
    public UserDataVO getUser(String username) {
        UserDataVO userDataBean = new UserDataVO();
        userDataBean.setUserName(username);

        // 获取用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("status", 0);
        SysUser sysUser = userDao.selectOne(queryWrapper);
        if (sysUser == null) {
            return null;
        }
        // 获取菜单信息
        userDataBean.setUserId(String.valueOf(sysUser.getUserId()));
        return userDataBean;
    }


    /**
     * 获取用户登录信息
     *
     * @author 佛冷
     * @date 2019/12/19
     */
    @DataSource(DataSourceType.MASTER)
    public UserInfoVO getUserInfo(String username) {

        // 获取用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("status", 0);
        SysUser sysUser = userDao.selectOne(queryWrapper);
        if (sysUser == null) {
            return null;
        }

        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setIcon("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1656864980,2857502437&fm=26&gp=0.jpg");
        userInfoVO.setUsername(sysUser.getUsername());
        userInfoVO.setRoles(Arrays.asList(sysUser.getUsername()));

        // 获取菜单信息
        List<SysPage> pageList = sysPageDao.listPage(sysUser.getUserId());
        userInfoVO.setMenus(pageList);
        return userInfoVO;
    }

}
